package com.wang.users.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;

import com.alibaba.fastjson.JSONObject;
import com.wang.common.pojo.InfoEnum;
import com.wang.common.service.BaseService;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.users.pojo.ALog;
import com.wang.users.service.IUsersDbService;
import com.wang.users.service.IUsersOperService;
import com.wang.users.service.impl.UsersDbServiceImpl;
import com.wang.users.service.impl.UsersOperServiceImpl;

/**
 * 用户模块的线程池
 * 
 * @author lby
 *
 */
public class UsersCallableUtil extends BaseService implements Callable<Object>
{
	/**
	 * 操作类型
	 */
	private String operType ;
	private IUsersDbService usersDbService = new UsersDbServiceImpl();
	private IUsersOperService usersOperService  = new UsersOperServiceImpl();
	private Map<String,Object>paramsMap;
	
	/**
	 * 用户模块所有和线程相关的代码进行放到这里
	 * 线程一启动会调用call方法,一个call方法如何处理不同的操作呢?
	 * Servlet每次访问会调用serivce方法,一个service方法如何处理不同的操作呢?
	 */
	@Override
	public Object call() throws Exception
	{
		String threadName = Thread.currentThread().toString() ; 
		ConstatFinalUtil.SYS_LOGGER.info("==线程启动:thread:{}==",threadName);
		if("saveLog".equalsIgnoreCase(this.operType)) {
			this.saveBatchLog();
		}else if("statResBatchInsert".equalsIgnoreCase(operType))
		{
			this.statResBatchInsert();
		}
		ConstatFinalUtil.SYS_LOGGER.info("==线程终止:thread:{}==",threadName);
		return null;
	}
	public void setUsersOperService(IUsersOperService usersOperService)
	{
		this.usersOperService = usersOperService;
	}
	/**
	 * 批量插入统计结果
	 */
	private void statResBatchInsert()
	{
		Date stDate = (Date)paramsMap.get("stDate");
		Date edDate = (Date)paramsMap.get("edDate");
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(stDate);
		/*循环*/
		while(calendar.getTime().before(edDate))
		{
			Date dateTemp = calendar.getTime();
			/*保存数据*/
			this.usersOperService.saveOneStatResService(dateTemp);
			calendar.add(Calendar.DATE, 1);
		}
	}
	
	/**
	 * 制作数据
	 */
	public void saveBatchLog()
	{
		String threadName = Thread.currentThread().toString() ;
		Random random = new Random();
		
		while(true)
		{
			try
			{
				/* 生成8-->29的随机数 */
				int adminsId = random.nextInt(29 - 7) + 8 ;
				if(adminsId == 30)
				{
					adminsId = 1 ; 
				}
				
				ALog log = new ALog();
				log.setAdminsId(adminsId);
				/* 登录类型 */
				log.setLogType(random.nextInt(6));
				/* 内容 */
				log.setContent(threadName + "===" + this.regexUtil.proccedRandStr(30, InfoEnum.VAR_NUMCHAR));
				/* 随机生成ip */
				StringBuffer ipSb = new StringBuffer();
				for(int i = 0 ; i < 4 ; i ++ ) {
					int ipTemp = random.nextInt(256);
					ipSb.append(ipTemp + ".");
				}
				/* 去掉最后一个.  */
				ipSb.delete(ipSb.lastIndexOf("."), ipSb.length());
				log.setIp(ipSb.toString());
				/* 状态 */
				log.setStatus(Byte.valueOf(random.nextInt(2) + ""));
				/* 10年以内的时间随机 */
				Date now = new Date();
				/* 单位秒 */
				int year10 = 1 * 60 * 60 * 24 * 365 * 5; 
				/* 生成10年以内的随机数 */
				int dateRand = random.nextInt(year10);
				long currLong = System.currentTimeMillis() ; 
				/* 随机减去 */
				currLong = currLong - Long.valueOf(dateRand) * 1000; 
				/* 设置时间;传入一个毫秒,变成哪年 */
				now.setTime(currLong);
				
				log.setCreateTime(now);
				log.setUpdateTime(now);
				log.setPubTime(now);
				
				/* 保存一条记录 */
				JSONObject resultJSON = this.usersDbService.saveOneLogService(log);
				ConstatFinalUtil.SYS_LOGGER.info("保存成功:{}",resultJSON.toJSONString());
				/* 记录日志
				 * adminId,logType,content,ip,status,createTime,updateTime,pubTime
				 *  */
				ConstatFinalUtil.SYS_LOGIN.info("{},{},{},{},{},{},{},{}",
						log.getAdminsId(),log.getLogType(),log.getContent(),log.getIp(),
						log.getStatus(),this.dateUtil.dateTimeToStr(log.getCreateTime()),
						this.dateUtil.dateTimeToStr(log.getUpdateTime()),
						this.dateUtil.dateTimeToStr(log.getPubTime()));
				/* 休眠100毫秒随机 */
				Thread.sleep(random.nextInt(4) * 10);
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	public void setOperType(String operType)
	{
		this.operType = operType;
	}
	public void setParamsMap(Map<String, Object> paramsMap)
	{
		this.paramsMap = paramsMap;
	} 
}
